package com.springboot2.service.Impl;

import com.springboot2.entity.Menu;
import com.springboot2.entity.Role;
import com.springboot2.entity.RoleMenu;
import com.springboot2.entity.User;
import com.springboot2.mapper.RoleMapper;
import com.springboot2.mapper.RoleMenuMapper;
import com.springboot2.mapper.RoleResourceMapper;
import com.springboot2.mapper.UserMapper;
import com.springboot2.service.MenuService;
import com.springboot2.service.ResourceService;
import com.springboot2.service.UserService;
import com.springboot2.utils.Result;
import com.springboot2.utils.TokenUtil;
import lombok.val;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper; 
    
    @Resource
    private MenuService menuService;

    @Resource
    private RoleResourceMapper roleResourceMapper;

    @Resource
    private ResourceService resourceService;

    @Override
    public void add(User user) {
        userMapper.add(user);
    }

    @Override
    public void del(int id) {
        userMapper.del(id);

    }

    @Override
    public void update(User user) {
        userMapper.update(user);

    }

    @Override
    public User findById(int id) {
        return userMapper.findById(id);
    }

    @Override
    public List<User> findByPage(int pageNo, int pageSize) {
        Map<String,Integer> map = new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        return userMapper.findByPage(map);
    }

    @Override
    public int count() {
        return userMapper.count();
    }

    @Override
    public List<User> findByPageParams(int pageNo, int pageSize, String name) {
        Map<String,Object> map = new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("name",name +"%");
        return userMapper.findByPageParams(map);
    }

    @Override
    public int countParams() {
        return userMapper.countParams();
    }

    @Override
    public Result login(String name, String password) {
        User user = userMapper.login(name);

        if(user == null){
            return Result.error("400","用户不存在",null);
        }else {
            if(user.getPassword().equals(password)){
                //先拿到 角色标记 --- 标记
                String flag = user.getRole();  //字符串
                // 根据标记flag 查出 角色id
                Integer roleId = roleMapper.findByFlag(flag);

                // 再根据 角色id  查询出这个角色对应有哪些菜单id
                List<Integer> menuIds = roleMenuMapper.findByRoleMenu(roleId);

                List<Integer> resourceIds = roleResourceMapper.findByRoleId(roleId);

                //根据系统查出所有的菜单
                List<Menu> menus = menuService.lists();

                List<com.springboot2.entity.Resource> resources = resourceService.findList();

                //new一个最后的筛选完成之后的list
                List<Menu> roleMenus = new ArrayList<>();

                //  根据当前用户角色菜单 筛选出 菜单项，之后，再添加到 new roleMenus
                for (Menu menu:menus){
                    if(menuIds.contains(menu.getId())){
                        roleMenus.add(menu);
                    }
                    List<Menu> children = menu.getChildren();
                    children.removeIf(child -> !menuIds.contains(child.getId()));
                }
                user.setMenus(roleMenus);

                List<com.springboot2.entity.Resource>  roleResources = new ArrayList<>();

                for (com.springboot2.entity.Resource resource:resources){
                    if(resourceIds.contains(resource.getId())){
                        roleResources.add(resource);
                    }
                    List<com.springboot2.entity.Resource> children = resource.getChildren();
                    children.removeIf(child -> !resourceIds.contains(child.getId()));
                }
                user.setPermission(roleResources);
                //添加了token 验证
                String token = TokenUtil.getToken(user.getId().toString(), user.getPassword());
                user.setToken(token);
                return Result.success(user);
            }else {
                return Result.error("402","用户或者密码",null);
            }
        }
    }

    @Override
    public int countParamsByName(String name) {
        Map<String,Object> map = new HashMap<>();
        map.put("name",name +"%");
        return userMapper.countParamsByName(map);
    }
}
